package org.l.qiao.el;

import org.l.qiao.annotation.Key;
import org.l.qiao.KeyType;
import org.l.qiao.LBean;
import org.l.qiao.Validate;
import org.l.qiao.base.Convert;
import org.l.qiao.base.LType;

/**
 * Created by 严秋旺 on 2015/5/13.
 */
public class Expression extends LBean {

    /**
     * EL表达式
     */
    @Key(keyType = KeyType.PropertyKey, check = Validate.EXPRESSION_KEY, valueType = LType.LTYPE_STRING, nullable = false)
    private String key;

    @Key(keyType = KeyType.PropertyKey, check = "", valueType = LType.LTYPE_STRING, nullable = false)
    private String value;

    @Key(keyType = KeyType.PropertyKey, check = Validate.INTEGER, valueType = LType.LTYPE_INTEGER, nullable = false)
    private int type;

    public Expression() {

    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    /**
     * 将字符串形式值转换
     *
     * @return 转换后的对象值
     */
    public Object parseValue() {
        Object object = Convert.parse(type, value);
        return object;
    }

    /**
     * 设置值
     *
     * @param o 对象形式
     */
    public void setValue(Object o) {
        if (o == null) {
            this.value = null;
            this.type = LType.LTYPE_STRING;
        } else {
            LType lType = Convert.getLType(o);
            this.value = lType.toString(o);
            this.type = lType.getLType();
        }
    }

}
